package com.k52group.vn.micromarket;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;
import com.k52group.vn.micromarket.classes.ContactInfo;
import com.k52group.vn.micromarket.utils.Constant;
import com.k52group.vn.micromarket.utils.HttpRequest;

public class ContactInfoActivity extends Activity {
	Button bt1, bt2, bt3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contactinfo);

		bt1 = (Button) findViewById(R.id.button1);
		bt2 = (Button) findViewById(R.id.button2);
		bt3 = (Button) findViewById(R.id.button3);

		new LoadDataTask().execute();
	}

	ContactInfo contactInfo = new ContactInfo();

	private class LoadDataTask extends AsyncTask<String, String, String> {
		ProgressDialog dialog;
		boolean isFalse = false;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(ContactInfoActivity.this);
			dialog.setMessage("Loading ...");
			dialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				HttpRequest request = HttpRequest.get(Constant.URL_CONTACT_INFO);
				String respone = request.body();
				System.out.println("respone data " + respone);
				contactInfo = new Gson().fromJson(respone, ContactInfo.class);
				if (contactInfo == null) {
					contactInfo = new ContactInfo();
				}
			} catch (Exception e) {
				isFalse = true;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			setView();
			if (isFalse)
				Toast.makeText(ContactInfoActivity.this, "Can't load data !", Toast.LENGTH_SHORT).show();
		}
	}

	private void setView() {
		bt1.setText(Html.fromHtml(contactInfo.getContact()));
		bt2.setText(Html.fromHtml(contactInfo.getNews()));
		bt3.setText(Html.fromHtml(contactInfo.getTerm()));
	}
}
